package bean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

@WebServlet(name = "ChangeName")
public class ChangeName extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String name=request.getParameter("name");
        String s1=null,str=null;
        BufferedReader input=null;
        File file=null;
        boolean flag=true;
        try
        {
            file=new File("person.txt");
            input=new BufferedReader(new FileReader(file));
            while((s1=input.readLine())!=null)
            {
                str=input.readLine();
                String s2[]=str.split(",");
                if(s2[0].equals(name)) flag=false;
            }
            input.close();
            if(flag)
            {
                User user=(User)request.getSession().getAttribute("LocalUser");
                user.setUsername(name);
                request.getSession().setAttribute("LocalUser",user);
                function.writeDgutToUser(user);
                response.sendRedirect("/interface.jsp");
            }
            else {
                response.sendRedirect("/dgut_makeName.jsp");
            }
        }
        catch(IOException e) {System.out.println("读取文件失败！");}
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}
